1141A - Game 23 - CodeForces Solution


implementation math *1000

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
d = 0
k = 0
if m%n != 0:
    print(-1)
else:
    d = m//n
    while d!= 1:
        if d %2 == 0:
            d =d//2
            k+=1
        elif d%3 == 0:
            d = d // 3
            k += 1
        else:

            d = 1
            k = -1
    print(k)



C++ Code:

using namespace std;
#include<bits/stdc++.h>
#include <iostream>
#include <cmath>
#define _DrIndex_ ios_base::sync_with_stdio(0);cin.tie(0),cout.tie(0);
#define clr(x,val) memset(x,val,sizeof(x))
#define read(FILE) freopen(FILE, "r", stdin);
#define write(FILE) freopen(FILE, "w", stdout);
#define ll long long
#define  ul unsigned int
#define ull unsigned long long
#define ld long double
void read_write();
/*===================================================*/
bool preceed(pair<char,int> x , pair<char,int> y) {return x > y;}
/*===================================================*/
bool sortbyCond(const pair<int, int>& a,
                const pair<int, int>& b)
{
    if (a.first != b.first)
        return (a.first < b.first);
    else
        return (a.second > b.second);
}
/*===================================================*/
int factDigits(int n){
    if(n<0)return 0;if(n<=1)return 1;double digits=0;
    for (int i = 2; i <= n; ++i) {
        digits+= log10(i);
    }
    return floor(digits)+1;
}
/*===================================================*/
void primeFactors(int n){int c=2;while(n>1){if(n%c==0){std::cout<<c<<" ";n/=c;}else c++;}}
/*===================================================*/
bool cmp(std::pair<std::string,int>&a,std::pair<std::string,int>&b){return a.second>b.second;}
/*===================================================*/
bool absPreceed(int x , int y){return abs(x)< abs(y);}
/*===================================================*/
ll noOfDigits(ll n){return 1+floor(log10(n));}
/*===================================================*/
ll gcd(ll a, ll b)
{
    if (a == 0) return b;
    return gcd(b%a, a);
}
/*===================================================*/
ll lcm(ll a, ll b)
{
    return (a / gcd(a, b))*b;
}
/*===================================================*/
// kadane's algorithm for getting max sum of sub array in O(n)
int maxSubArraySum(int array[], int size)
{
    int max_so_far = INT_MIN, max_ending_here = 0;

    for (int i = 0; i < size; i++)
    {
        max_ending_here = max_ending_here + array[i];
        if (max_so_far < max_ending_here)
            max_so_far = max_ending_here;

        if (max_ending_here < 0)
            max_ending_here = 0;
    }
    return max_so_far;
}

/*===================================================*/
long long MOD = 1e9 + 7;

ll fast_power(ll base , ll power){
    ll  res=1;
    while (power > 0){
        if(power & 1){
            res=(res*base)%MOD;
        }
        base = (base*base) % MOD;
        power >>= 1;
    }
    return res;
}
/*===================================================*/
// get no. ones in the binary representation
int popCount(int num){
    int res=0;
    while(num > 0){
        res += num & 1;
        num >>= 1;
    }return res;
}
/*===================================================*/

const int N = 1e6+5;
int n,m,ans=-1;
void gen(int cnt , int num){
    if(num >= m) {if(num == m)ans=cnt;return;}
    gen(cnt+1,num*2);
    gen(cnt+1,num*3);
}
int main() {
    _DrIndex_
    read_write();
    int tt=1;//cin>>tt;
    while (tt--) {
        cin >> n >> m;
        gen(0,n);
        cout << ans;
    }
    return 0;
}
void read_write() {


#ifndef ONLINE_JUDGE
    read("../input.txt")
    //write("../output.txt")
#  endif



}


Comments

Submit
0 Comments
More Questions

779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II
238. Product of Array Except Self
229. Majority Element II